Skip to content

fix(basic): pace domains by the longest robots.txt Crawl-delay - #4493

Open
wasabihu wants to merge 2 commits into
apify:masterfrom
wasabihu:fix/throttling-max-crawl-delay-4491
Open

wasabihu wants to merge 2 commits into
apify:masterfrom
wasabihu:fix/throttling-max-crawl-delay-4491

Conversation

@wasabihu

Copy link
Copy Markdown

Fixes #4491

Summary

  • Track lastDispatchedAt per DomainState in packages/basic-crawler/src/internals/throttling-request-manager.ts (restoring it when no request is dequeued and resetting it when the queue is purged).
  • Update ThrottlingRequestManager.#recordDeclaredInterval to retain the longest Crawl-delay recorded for a domain, rather than keeping only the first signal.
  • When a longer Crawl-delay arrives after a request has already been dispatched for a domain, extend crawlDelayUntil from lastDispatchedAt + effectiveDelayMs (while preserving minCrawlDelaySecs when it is longer).

Tests

  • Added 7 regression and negative-control tests in test/basic-crawler/throttling-request-manager.test.ts covering:
    • a longer Crawl-delay replacing an earlier shorter one across origins sharing a domain clock (subdomains under registrableDomain, and different schemes or ports under hostname),
    • extending an active cooldown from the last dispatch (including after an initially unthrottled first dispatch),
    • shorter subsequent signals preserving the longer delay, minCrawlDelaySecs floor behavior, and purge() cleanly resetting dispatch timestamps.
  • Verified locally after the final edit:
    • vitest run test/basic-crawler/throttling-request-manager.test.ts test/core/crawlers/basic-crawler.test.ts test/core/request-manager-tandem.test.ts: 3 files passed (294 tests passed).
    • Workspace package builds (tsc -p tsconfig.build.json and scripts/copy.ts), tsc --noEmit --project test/tsconfig.json, oxlint --type-aware, and oxfmt --check on the changed files all passed with 0 errors and 0 warnings.

Note: This pull request was prepared with AI-assisted coding and verified locally against the test suite, TypeScript compiler, linter, and formatter.

@github-actions github-actions Bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Nov 26, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T02:06:05.811922Z b4d547a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b4d547a30c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


// No dispatch to pace, so the domain keeps its slot.
state.crawlDelayUntil = crawlDelayUntilBefore;
state.lastDispatchedAt = lastDispatchedAtBefore;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve newer dispatch timestamps when an empty fetch completes

When minCrawlDelaySecs is 0 and same-domain fetches overlap before a crawl delay is known, an older fetch can return null after a newer fetch successfully dequeues a request. This unconditional rollback restores the older snapshot, potentially resetting lastDispatchedAt to null and erasing the successful dispatch. A subsequent robots.txt signal then skips or shortens the cooldown extension in #recordDeclaredInterval, allowing another request before the declared interval has elapsed. Rollback must preserve dispatch timestamps established by newer fetches.

Useful? React with 👍 / 👎.

@wasabihu

Copy link
Copy Markdown
Author

Addressed the review finding in follow-up commit 8f0ea8203a8ff7f94a70510fb565dabc4bc3eeb7:

  • Only record confirmed dispatches in lastDispatchedAt: stopped writing state.lastDispatchedAt speculatively before await subManager.fetchNextRequest(). It is now updated solely when a real request is returned (Math.max(state.lastDispatchedAt, now)), and crawlDelayUntil is reconciled against any Crawl-delay that arrived mid-flight.
  • Guard crawlDelayUntil rollback with a per-domain monotonic reservation token (crawlDelayReservation) inside try / finally: empty (null) or rejected fetches only roll back when their own reservation is still current, preserving any newer dispatch/reservation or #purgeDomainQueues() reset as well as any mid-flight Crawl-delay extension anchored to an earlier confirmed dispatch.
  • Regression tests: added 5 tests covering overlapping minCrawlDelaySecs: 0 fetches (cross-tick and same-tick), rejected fetches + stale slow fetch completion, mid-flight Crawl-delay preservation, and the single empty-fetch negative control (86/86 in throttling-request-manager.test.ts, 137/137 in test/basic-crawler/, 288/288 in related core crawler/queue suites; tsc, oxlint, oxfmt --check clean).

@janbuchar
janbuchar self-requested a review November 26, 2026 10:41
vdusek pushed a commit to apify/crawlee-python that referenced this pull request Nov 28, 2026
### Description

When one domain gets crawl-delays from several robots.txt files,
`ThrottlingRequestManager` now keeps the longest one instead of the
first. Several files cover one domain for a site and its subdomains
under `throttle_by='registrable_domain'`, and for HTTP and HTTPS of one
host. A robots.txt re-fetch still can't speed a domain up, it can only
slow it down.

The same change for Crawlee JS is tracked in apify/crawlee#4491, with a
fix proposed in apify/crawlee#4493.

### Issues

- Follow-up: #2507

*✍️ Drafted by Claude Code*
vdusek pushed a commit to apify/crawlee-python that referenced this pull request Nov 29, 2026
### Description

When one domain gets crawl-delays from several robots.txt files,
`ThrottlingRequestManager` now keeps the longest one instead of the
first. Several files cover one domain for a site and its subdomains
under `throttle_by='registrable_domain'`, and for HTTP and HTTPS of one
host. A robots.txt re-fetch still can't speed a domain up, it can only
slow it down.

The same change for Crawlee JS is tracked in apify/crawlee#4491, with a
fix proposed in apify/crawlee#4493.

### Issues

- Follow-up: #2507

*✍️ Drafted by Claude Code*
…ch rollback

Avoid writing lastDispatchedAt before a sub-queue fetch actually returns a request, and guard crawlDelayUntil rollback with a per-domain monotonic reservation token so an older empty or rejected fetch cannot overwrite a newer concurrent dispatch or purge.
@wasabihu
wasabihu force-pushed the fix/throttling-max-crawl-delay-4491 branch from 8f0ea82 to f0c4e81 Compare December 1, 2026 13:34

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-tooling Issues with this label are in the ownership of the tooling team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v4: ThrottlingRequestManager paces a site by the first robots.txt Crawl-delay it reads

1 participant